
package inf3791.calendarsystem.others.components.calendar;

import inf3791.calendarsystem.shared.Appointment;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Jonas Weber
 */
class AppointmentRenderer extends JLabel implements TableCellRenderer {

    public AppointmentRenderer() {
        //setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 24));
        setOpaque(true);
    }
    private boolean isRed = false;
    private boolean isGreen = false;

    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object appointmentwrapper, boolean isSel, boolean hasFoc, int row, int col) {
        setFont(jtable.getFont().deriveFont(Font.PLAIN));
        if (appointmentwrapper != null) {
            setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            AppointmentWrapper aw = (AppointmentWrapper) appointmentwrapper;
            setText(String.valueOf(aw.dayspec.day));
            setForeground(aw.isCurrentMonth ? Color.BLACK : Color.LIGHT_GRAY);
            if (isSel) {
                setBackground(new Color(0x707070));
            } else {
                if (aw.isCurrentMonth) {
                    setBackground(new Color(0xC0C0C0));
                } else {
                    setBackground(Color.WHITE);
                }
            }
            setHorizontalAlignment(SwingConstants.CENTER);
            if (aw.apps != null) {
                for (Appointment a : aw.apps) {
                    if (a.getAppointmentType() == Appointment.AppointmentType.MEDICAL) {
                        isRed = true;
                    } else {
                        isGreen = true;
                    }
                }
            }
        } else {
            setText("N");
        }
        return this;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int xbound = getWidth();
        int ybound = getHeight();
        if (isRed) {
            g.setColor(Color.RED);
            g.fillPolygon(new int[]{xbound, xbound - 20, xbound}, new int[]{0, 0, 20}, 3);
        }
        if (isGreen) {
            g.setColor(Color.GREEN);
            g.fillPolygon(new int[]{0, 20, 0}, new int[]{ybound, ybound, ybound - 20}, 3);
        }
    }

}
